<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 登录表单 start -->

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">{{ title }}</h3>
        </div>
        <!-- 账号、用户名 start -->
        <el-form-item prop="mobile">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="mobile"
            v-model="loginForm.mobile"
            placeholder="phone"
            name="mobile"
            type="text"
            maxlength="11"
            data-index="1"
            auto-complete="on"
          />
        </el-form-item>
        <!-- 账号、用户名 end -->

        <!-- 密码 start -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            data-index="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <!-- 密码 end -->

        <!-- 登录按钮 start -->
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
          >登 录</el-button
        >
        <!-- 登录按钮 end -->

        <div class="tips">
          <span style="margin-right: 20px">账号: 13800000004</span>
          <span> 密码: 123456</span>
        </div>
      </el-form>
      <!-- 登录表单 start -->
    </div>
  </div>
</template>

<script>
import { validMobile } from "@/utils/validate";
import defaultSettings from "@/settings";
import msgTip from "@/utils/message-tip";

export default {
  name: "Login",
  data() {
    //自定义校验规则====手机号
    const validateMobile = (rule, value, callback) => {
      if (!validMobile(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    //自定义校验规则====密码
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不少于6位"));
      } else {
        callback();
      }
    };
    return {
      //标题
      title: defaultSettings.title,
      //登录表单
      loginForm: {
        mobile: "13600000001",
        password: "123456",
      },
      //表单校验规则
      loginRules: {
        mobile: [
          { required: true, trigger: "blur", validator: validateMobile },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      //登录按钮Loading标识
      loading: false,
      //密码框type
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    /**
     * 显示密码
     */
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    /**
     * 登录
     */
    handleLogin() {
      this.$refs.loginFormRef.validate(async(valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/loginActions", this.loginForm)
            .then((res) => {
              this.$router.push({ path: this.redirect || "/" });
              msgTip({ msg: res });
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

/* reset element-ui css */
.login-container {
  .el-input {
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      height: 40px;
    }
  }

  .el-form-item {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    // border-bottom: 1px solid ;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$bg: "../../assets/bg.svg";
// 登录表单容器
.login-container {
  min-height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: url($bg) no-repeat;
  .login-box {
    min-width: 300px;
    width: 400px;
    max-width: 400px;
    margin-right: 20px;
  }
  // 登录表单
  .login-form {
    position: relative;
    box-shadow: 0 0 15px 10px rgba(0, 0, 0, 0.1);
    background-color: #fff;
    padding: 20px 40px;
    margin: 0 auto;
    border-radius: 10px;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .tips {
    font-size: 14px;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
